﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class RYGL : Form
    {
        DataTable dt = new DataTable();
        public RYGL()
        {
            InitializeComponent();
        }

        private void ADD_Click(object sender, EventArgs e)
        {

        }

        private void RYGL_Load(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {
                    DataTable dt_qx = new DataTable();
                    SqlDataAdapter da = new SqlDataAdapter();//实例化sqldataadpte
                    SqlCommand cmd1 = new SqlCommand("SELECT UID 用户名,PAW 密码,NAME 姓名,QXMC 权限名称 FROM ZD_RY", con);//显示人员
                    SqlCommand cmd2 = new SqlCommand("SELECT MC 权限名称 FROM ZD_QX", con);//显示权限

                    da.SelectCommand = cmd1;//设置为已实例化SqlDataAdapter的查询命令
                    da.Fill(dt);
                    da.SelectCommand = cmd2;
                    da.Fill(dt_qx);
                    dataGridViewX1.DataSource = dt;
                    dataGridViewX2.DataSource = dt_qx;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void ADD_Click_1(object sender, EventArgs e)
        {
            dataGridViewX1.CurrentCell = this.dataGridViewX1.Rows[this.dataGridViewX1.Rows.Count - 1].Cells[0];
            dataGridViewX1.CurrentRow.Cells[1].Value = "";
            dataGridViewX1.CurrentRow.Cells[3].Value = "";

        }

        private void DELETE_Click(object sender, EventArgs e)
        {
            try
            {
                dataGridViewX1.Rows.Remove(dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex]);
            }
            catch { }
        }

        private void SAVE_Click(object sender, EventArgs e)
        {
            if (dt != null)
            {
                try
                {
                    using (SqlConnection con = new SqlConnection(SQL.connectDB))
                    {
                        SqlDataAdapter uda = new SqlDataAdapter("SELECT UID 用户名,PAW 密码,NAME 姓名,QXMC 权限名称 FROM ZD_RY", con);
                        SqlCommandBuilder scb = new SqlCommandBuilder(uda);
                        uda.Update(dt);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            MessageBox.Show("保存成功！", "系统提示");
        }

        private void dataGridViewX1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (dataGridViewX1.CurrentCell.ColumnIndex == 3)
            {
                dataGridViewX2.Size = new Size(dataGridViewX2.Columns[0].Width + 8, 168);
                groupBox1.Visible = true;
            }
        }

        private void dataGridViewX2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            dataGridViewX1.CurrentCell.Value = dataGridViewX2.CurrentCell.Value;
            groupBox1.Visible = false;
        }
    }
}